package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.activation.MimetypesFileTypeMap;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;

public class UnZip {
	
	List<String> fileList;

	private MimetypesFileTypeMap fileType;
	private final String ZIP_FILE_TYPE ="application/octet-stream";
	
	public UnZip() {
		fileType = new MimetypesFileTypeMap();
	}
	
	/**
	 * Unzip it
	 * 
	 * @param zipFile
	 *            input zip file
	 * @param output
	 *            zip file output folder
	 *            
	 * This method is from
	 * http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/
	 */
//	public void unZipIt(String zipFile, String outputFolder) {
//
//		byte[] buffer = new byte[1024];
//
//		try {
//
//			// create output directory is not exists
//			File folder = new File(outputFolder);
//			if (!folder.exists()) {
//				folder.mkdir();
//			}
//
//			// get the zip file content
//			ZipInputStream zis = new ZipInputStream(
//					new FileInputStream(zipFile));
//			// get the zipped file list entry
//			ZipEntry ze = zis.getNextEntry();
//
//			while (ze != null) {
//
//				String fileName = ze.getName();
//				File newFile = new File(outputFolder + File.separator
//						+ fileName);
//
//				System.out.println("file unzip : " + newFile.getAbsoluteFile());
//				
//				// create all non exists folders
//				// else you will hit FileNotFoundException for compressed folder
//				File parent = newFile.getParentFile();
//				parent.mkdirs();
//				parent.setExecutable(true);
//				
//				//System.out.println("dir1:" + newFile.getParentFile().exists());
//				//System.out.println("dir2:" + newFile.getParentFile().canWrite());
//				System.out.println("dir3:" + newFile.getParentFile().canExecute());
//				
//				System.out.println("dir3-1:" + ze.isDirectory());
//				
//				
//				System.out.println("dir4:" + newFile.isDirectory());
//				System.out.println("dir5:" + newFile.isFile());
//				System.out.println("dir6:" + newFile.isAbsolute());
//				System.out.println("dir7:" + newFile.isHidden());
//				
//				if (!newFile.isFile())
//				    newFile.createNewFile();
//				
//				System.out.println("dir3-1:" + newFile.getParentFile().canExecute());
//				System.out.println("dir4:" + newFile.isDirectory());
//				System.out.println("dir5:" + newFile.isFile());
//				System.out.println("dir6:" + newFile.isAbsolute());
//				System.out.println("dir7:" + newFile.isHidden());
//				
//				FileOutputStream fos = new FileOutputStream(newFile);
//
//				int len;
//				while ((len = zis.read(buffer)) > 0) {
//					fos.write(buffer, 0, len);
//				}
//
//				fos.close();
//				ze = zis.getNextEntry();
//			}
//
//			zis.closeEntry();
//			zis.close();
//
//			System.out.println("Done");
//
//		} catch (IOException ex) {
//			ex.printStackTrace();
//		}
//	}
//
	public void listZipFilesForFolder(File folder, List<String> allZipFiles) {
	    for (final File fileEntry : folder.listFiles()) {
	        if (fileEntry.isDirectory()) {
	        	listZipFilesForFolder(fileEntry, allZipFiles);
	        } else {
//	        	System.out.println(fileType.getContentType(fileEntry));
	        	String fileName = fileEntry.getName();
	        	String[] splitter = fileName.split("\\.");
	        	String extension = splitter[splitter.length-1];
//	        	System.out.println(extension);
	            if(ZIP_FILE_TYPE.equals(fileType.getContentType(fileEntry)) 
	            		&& extension.equalsIgnoreCase("zip")) {
	        		System.out.println(fileEntry.getAbsolutePath());
	        		allZipFiles.add(fileEntry.getAbsolutePath());
	        	}
	        }
	    }
	}
	
	public void extractByLoopAllFiles(String inputZipFile, String outputFolder, boolean deleteInput) {
		
		try {
			// Initiate ZipFile object with the path/name of the zip file.
			ZipFile zipFile = new ZipFile(inputZipFile);
			
			if(zipFile.isValidZipFile()) {
				// Get the list of file headers from the zip file
				List fileHeaderList = zipFile.getFileHeaders();
				
				// Loop through the file headers
				for (int i = 0; i < fileHeaderList.size(); i++) {
					FileHeader fileHeader = (FileHeader)fileHeaderList.get(i);
					// Extract the file to the specified destination
					zipFile.extractFile(fileHeader, outputFolder);
				}
				
				//delete when done
				if(deleteInput) {
					File zipInputFile = new File(inputZipFile);
					MovingFile.delete(zipInputFile);
				}
			}
			
		} catch (ZipException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}